/**
 * Copyright © 2011 looty - Textoo
 * @link http://www.textoo.org
 * @link http://code.google.com/p/textoo/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.core.textoo.context.local 
{
	import flash.events.*;
	import flash.utils.*;
	import org.looty.log.*;
	import org.looty.textoo.events.*;
	
	public class LocalisationText extends EventDispatcher
	{
		
		private var _texts					:Vector.<String>;
		private var _classNames				:Vector.<String>;
		private var _defaultClassName		:String;
		private var _key					:String;
		private var _index					:uint;
		
		static private const LOCALISATION_UPDATE		:TextooEvent = new TextooEvent(TextooEvent.LOCALISATION_UPDATE);
		
		public function LocalisationText(key:String, length:uint) 
		{
			_key = key;
			
			_defaultClassName = "";
			_texts = new Vector.<String>(length, true);
			_classNames = new Vector.<String>(length, true);
			
			for (var i:int = 0; i < length; i++) 
			{
				_texts[i] = _key
				_classNames[i] = "";
			}
		}
		
		public function populate(index:uint, text:String, className:String = ""):void
		{
			if (_texts[index] != _key) Looger.error("duplicate text key : " + _key);
			
			_texts[index] = text;			
			
			if (className != "") _classNames[index] = className;
			
			dispatchEvent(LOCALISATION_UPDATE);
		}
		
		public function get text():String
		{
			return _texts[_index];
		}
		
		public function get className():String
		{
			switch(true)
			{
				case _classNames[_index] != "" :
					return _classNames[_index];
				break;
				
				case _defaultClassName != "" :
					return _defaultClassName;
				break;
				
				default:
				return "";				
			}
		}
		
		public function get span():String
		{
			var name:String = className;
			return "<span " + (name != "" ? "class='" + name + "' " : "") + ">" + text + "</span>";
		}
		
		public function setLength(value:uint):void 
		{
			_texts.fixed = false;
			_texts.length = value;
			_texts.fixed = true;
			
			_classNames.fixed = false;
			_classNames.length = value;
			_classNames.fixed = true;
			
			for (var i:int = 0; i < value; i++) 
			{
				if (_texts[i] == null) _texts[i] = _key
				if (_classNames[i] == null) _classNames[i] = "";
			}
		}		
		
		public function setDefaultClassName(className:String):void
		{
			if (_defaultClassName == className) return;
			_defaultClassName = className;
			dispatchEvent(LOCALISATION_UPDATE);
		}		
		
		public function get defaultClassName():String 
		{
			return _defaultClassName;
		}
		
		public function get key():String 
		{
			return _key;
		}
		
		public function get index():uint 
		{
			return _index;
		}
		
		public function set index(value:uint):void 
		{
			_index = value;
			dispatchEvent(LOCALISATION_UPDATE);
		}
		
		public function dispose():void
		{
			
		}
		
		public function dump():String 
		{
			return "key : " + _key + " - texts : \"" + _texts.join("\", \"") + "\"";
		}
		
		override public function toString():String 
		{
			return "[" + getQualifiedClassName(this) + "] " + dump(); 
		}
		
	}

}